home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / msdos / fsr.zip / FSR.CPP < prev    next >
C/C++ Source or Header  |  1993-09-16  |  6KB  |  165 lines

  1. // File Search and Replace
  2. // This routine will scan a DOS file searching and replacing
  3. // matching text strings. The file to be opened will be specified
  4. // on the command line, and the new file that will be created will
  5. // be called FSR.NEW. After completing the operating and closing
  6. // both files, the original file will be replaced and FSR.NEW will
  7. // be renamed the same as the old file. As an Option you can pass
  8. // argc[4] as the TempFile name and this will be used instead of
  9. // FSR.NEW.
  10. // Command Line: FILENAME SEARCHSTRING REPLACESTRING [TempFile]
  11.  
  12. // FSR.CPP - By Todd Osborne CIS ID: 71431,2243
  13. // 09/16/93 - Compiled with Microsoft Visual C++ 1.00
  14.  
  15. // This source code and program are Public Domain
  16.  
  17. // Feel free to copy and use this source code in other programs.
  18. // Please let me know if you find any bugs or want improvements!!
  19.  
  20. // **** THIS PROGRAM IS CASE-SENSITIVE ****
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. // Function Prototypes
  27. int     FileSearchAndReplace (char*, char*, char*, char*);
  28. void     ReplaceString (char*, char *, char *);
  29. int     CopyTextFile (char*, char *);
  30. void    ShowHelp(void);
  31.  
  32. void main (int argv, char *argc[] )
  33. {
  34.     if (argv < 4)
  35.         {
  36.         ShowHelp();
  37.         exit (0);
  38.         }
  39.     else
  40.     
  41.     //argc[4] can be used to specify the temp file name (FSR.NEW is default)
  42.     if (argc[4]==NULL)
  43.         {
  44.         FileSearchAndReplace (argc[1], argc[2], argc[3], "FSR.NEW");
  45.         }
  46.         else FileSearchAndReplace (argc[1], argc[2], argc[3], argc[4]);
  47.     exit (0);
  48. }
  49.  
  50.  
  51. int FileSearchAndReplace (char *FileName, char *SearchString, char *ReplaceStr, char *TempFile)
  52. {
  53.     // Perform the File Search and Replace
  54.     // Function returns 1 if successfull, 0 if not
  55.     
  56.     FILE *FP1, *FP2;                        // File Pointer 1 (Open File) and 2 (New File)
  57.     char InString[256]="";
  58.     char *loc="";                            // Pointer to character location in InString
  59.         
  60.     if ( (FP1=fopen(FileName, "r")) != NULL )
  61.         {
  62.         //Open Output File
  63.         if ( (FP2=fopen(TempFile, "w")) == NULL )
  64.             {
  65.             fcloseall();                // Something's Wrong!!
  66.             return (0);
  67.             }
  68.         
  69.         // Process this file
  70.         while ( (fgets (InString, 256, FP1)) != NULL )
  71.             {
  72.             //There may be more than one string in each line, so loop
  73.             while ( (loc=strstr(InString, SearchString)) != NULL )
  74.                 {
  75.                 ReplaceString (InString, SearchString, ReplaceStr);
  76.                 }    
  77.             fputs (InString, FP2);        // Write modified line to new file
  78.             }
  79.         fcloseall();                    // Close open files
  80.         
  81.         // Copy File instead of rename. If you only rename it, the file
  82.         // will exists in the same directory as FSR.EXE. We want it to
  83.         // be placed in the same location it came from!
  84.         CopyTextFile (TempFile, FileName);
  85.         remove (TempFile);
  86.         return (1);
  87.         }
  88.     else 
  89.         {
  90.         printf("\nFile not found.\n");
  91.         return (0);
  92.         }
  93. }
  94.  
  95.  
  96. void ReplaceString (char *InString, char *SearchString, char *ReplaceStr)
  97. {
  98.     //Do the Actual String Replacement Here
  99.     
  100.     char NewString1[256]="";                // 1st Part of New String
  101.     char NewString2[256]="";                // 2nd Part of New String
  102.     char *loc="";
  103.     
  104.     loc=strstr(InString, SearchString);
  105.     strncpy (NewString1, InString, loc-InString);
  106.     strncpy (NewString2, InString + ( (loc-InString) + (strlen(SearchString)) ), 256);
  107.     strcpy (InString, NewString1);            // Combine these suckers
  108.     strcat (InString, ReplaceStr);          //
  109.     strcat (InString, NewString2);          //
  110. }
  111.  
  112.  
  113. int CopyTextFile (char *SourceFile, char *DestFile)
  114. {
  115.     // This routine copies TEXT files ONLY !!
  116.     
  117.     FILE *FP1, *FP2;
  118.     char InString[256]="";
  119.     
  120.     if ( (FP1=fopen(SourceFile, "r")) != NULL )
  121.         {
  122.         //Open Output File
  123.         if ( (FP2=fopen(DestFile, "w")) == NULL )
  124.             {
  125.             fcloseall();                // Something's Wrong!!
  126.             return (0);
  127.             }
  128.         else
  129.         // Process this file
  130.             while ( (fgets (InString, 256, FP1)) != NULL )
  131.                 {
  132.                 fputs (InString, FP2);
  133.                 }
  134.         fcloseall();
  135.         return (1);
  136.         }        
  137.     else return (0);
  138. }
  139.  
  140.  
  141. void ShowHelp(void)
  142. {
  143.     int i=0;
  144.     
  145.     printf("\n%c", 201);
  146.     for (i=2; i<80; i++) printf("%c", 205);
  147.     printf("%c", 187);
  148.     printf("%c                            File Search and Replace                           %c", 186, 186);
  149.     printf("%c", 204);
  150.     for (i=2; i<80; i++) printf("%c", 205);
  151.     printf("%c", 185);
  152.     printf("%c                                                                              %c", 186, 186);
  153.     printf("%c Command Line Argument: FN SS SR [TF]                                         %c", 186, 186);
  154.     printf("%c                                                                              %c", 186, 186);
  155.     printf("%c FN = File Name to Search     SS = String to Search For                       %c", 186, 186);
  156.     printf("%c SR = Replacement String      TF = Optional Temporary File Name               %c", 186, 186);
  157.     printf("%c                                                                              %c", 186, 186);
  158.     printf("%c   Public Domain Software By Todd Osborne: CIS ID 71431,2243 Charlotte, NC    %c", 186, 186);
  159.     printf("%c   Date: 09/16/93   Version 1.00   Coded using Microsoft Visual C++  V1.00    %c", 186, 186);
  160.     printf("%c                                                                              %c", 186, 186);
  161.     printf("%c NOTE: This Program is Case-Sensitive (Example: Hello is the same as hello)   %c", 186, 186);
  162.     printf("%c", 200);
  163.     for (i=2; i<80; i++) printf("%c", 205);
  164.     printf("%c\ndir ", 188);
  165. }